package com.ycy.dynamic;

import com.ycy.BaseTest;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.dynamic.DynamicUserTaskBuilder;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AfterSignUserTaskCmd2Demo extends BaseTest {

  public static final String processInstanceId = "110001";

  public static final String tasInstancekId = "110018";

  @Test
  public void createDef() {
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().addClasspathResource("dynamic/easy_key.bpmn20.xml").deploy();
//    Deployment deploy = repositoryService.createDeployment().addClasspathResource("dynamic/parallels_key.bpmn20.xml").deploy();
    System.out.println(deploy.getId());
  }

  @Test
  public void creatProcInst() {
    RuntimeService runtimeService = processEngine.getRuntimeService();
    HashMap<String, Object> variables = new HashMap<>();
    List<String> zhixingrens = new ArrayList<>();
    zhixingrens.add("ycy");
    zhixingrens.add("xdm");
    variables.put("ASSIGNEE_zx_LIST", zhixingrens);
    ProcessInstance processInstance =
            runtimeService.startProcessInstanceByKey("easy_key_1", variables);
//    ProcessInstance processInstance =
//            runtimeService.startProcessInstanceByKey("parallels_key", variables);
    System.out.println(processInstance.getProcessInstanceId());
  }

  /**
   * 在当前审批节点添加 审批节点
   */
  @Test
  public void addBeforePass() {
    DynamicUserTaskBuilder dynamicUserTaskBuilder = new DynamicUserTaskBuilder();
    Void unused = processEngine.getManagementService().executeCommand(
            new AfterSignUserTaskCmd2(dynamicUserTaskBuilder, tasInstancekId, processInstanceId));
  }

  /**
   * 查询流程实例下的所有任务
   */
  @Test
  public void queryProcessInstanceIdTask() {
    TaskService taskService = processEngine.getTaskService();
    //获取流程实例中的task
    List<Task> list = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
    for (Task task : list) {
      System.out.println(task);
      //查询所有参数
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  @Test
  public void queryHistory() {
    List<HistoricTaskInstance> list = processEngine.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).list();
    for (HistoricTaskInstance historicTaskInstance : list) {
      System.out.println(historicTaskInstance);
    }
  }

  @Test
  public void qu() {
    ExecutionEntityManager executionEntityManager =
            ((ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration()).getExecutionEntityManager();
    List<ExecutionEntity> childExecutionsByProcessInstanceId = executionEntityManager.findChildExecutionsByProcessInstanceId(processInstanceId);
    for (ExecutionEntity executionEntity : childExecutionsByProcessInstanceId) {
      System.out.println(executionEntity);
    }
  }


  @Test
  public void pass() {
//    taskService.complete("12510");
    taskService.complete("7529");
  }

  @Test
  public void pass2() {
    HashMap<String, Object> variables = new HashMap<>();
    List<String> baorong1 = new ArrayList<>();
    baorong1.add("ycy1");
    variables.put("ASSIGNEE_br1_LIST", baorong1);
    List<String> baorong2 = new ArrayList<>();
    baorong2.add("xdm1");
    variables.put("ASSIGNEE_br2_LIST", baorong2);
    taskService.complete("2518", variables);
  }

}
